/**
 * 总管理器
 */
cc.Class({
    extends: cc.Component,

    properties: {

    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.leftPlayerID = "";
        this.rightPlayerID = "";
    },

    start () {
        //面板字典
        this._panelDic = new Array();
        this._inviteRoomManager = this.registManager("InviteRoomPanel", "InviteRoomManager");
        this._currentPanel = null;
        this.msgHandler();
    },

    // update (dt) {},

    /**
     * 注册一个管理器
     * @param {string} nodeName 节点名称
     * @param {string} managerName 管理器名称
     * @returns 管理器变量
     */
    registManager (nodeName, managerName) {
        var manager = this.node.getChildByName(nodeName).getComponent(managerName);
        manager.mainManager = this;
        this._panelDic[nodeName] = manager;
        //默认隐藏
        manager.node.active = false;
        return manager;
    },

    /**
     * 转到对应的面板上
     * @param {string} dstPanel 目标面板名称
     */
    changePanel (dstPanel) {
        if (this._currentPanel) {
            this._currentPanel.node.active = false;
        }
        this._currentPanel = this._panelDic[dstPanel];
        if (this._currentPanel) {
            this._currentPanel.node.active = true;
            //初始化
            this._currentPanel.init();
        }

    },

    /**
     * 消息处理器
     */
    msgHandler () {
        wx.onMessage(data => {
            switch (data.message) {
                case 'Close':
                    this.changePanel('MenuPanel');
                    this.leftPlayerID = "";
                    this.rightPlayerID = "";
                    break;
                case 'ShowInviteRoom':
                    this.changePanel('InviteRoomPanel');
                    this.showInviteRoom(data.left, data.right);
                    break;
            }
        });
    },

    /**
     * 接收到显示房间信息的处理
     */
    showInviteRoom (left, right) {
        // if (left != this.leftPlayerID) {
        //     this._inviteRoomManager.showPlayerInfo(0, left);
        //     this.leftPlayerID = left;
        // }
        // if (right != this.rightPlayerID) {
        //     this._inviteRoomManager.showPlayerInfo(1, right);
        //     this.rightPlayerID = right;
        // }
        console.log("left"+left);
        console.log("right"+right);
        if (left != "") {
            this._inviteRoomManager.showPlayerInfo(0, left);
            this.leftPlayerID = left;
        }
        if (right != "") {
            this._inviteRoomManager.showPlayerInfo(1, right);
            this.rightPlayerID = right;
        }
    },
});
